package br.com.jvet.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter("cnpjCpfConvert")
public class CnpjCpfConvert implements Converter {

	public Object getAsObject(final FacesContext context,
			final UIComponent component, final String value) {
		String str = (String) value;
		str = str.replace(".", "").replace(".", "").replace("-", "").replace(
				"/", "");
		return str;
	}

	public String getAsString(final FacesContext context,
			final UIComponent component, Object value) {
		String str = (String) value.toString().trim();
		if ((str.equals("")) || (str.trim().equals(""))) {
			return str = "00.000.000/0000-00";
		}
		if (str.length() != 14) {
			return str = str.substring(0, 3) + "." + str.substring(3, 6) + "."
					+ str.substring(6, 9) + "-" + str.substring(9, 11);
		} else {
			return str = str.substring(0, 2) + "." + str.substring(2, 5) + "."
					+ str.substring(5, 8) + "/" + str.substring(8, 12) + "-"
					+ str.substring(12, 14);
		}
	}
}
